#include <iostream>
#include <string>
#include <queue>
#include <unordered_map>

using namespace std;

string origin, target;
unordered_map<string, int> m;
const int dir[6] = {-3, -2, -1, 1, 2, 3};
string::size_type size;

int bfs() {

    queue<string> q;
    q.push(origin);
    m[origin] = 0;

    while (q.size()) {
        string t = q.front();
        q.pop();
        if (t == target) {
            return m[t];
        }
        int d = m[t];
        int k = t.find('*');
        for (int i = 0; i < 6; ++i) {
            int nk = k + dir[i];
            if (nk < 0 || nk > size - 1) {
                continue;
            }
            swap(t[k], t[nk]);
            if (!m.count(t)) {
                q.push(t);
                m[t] = d + 1;
            }
            swap(t[k], t[nk]);
        }
    }

    return -1;
}

int main() {
    getline(cin, origin);
    getline(cin, target);

    size = origin.size();
    cout << bfs() << endl;

    return 0;
} 